class Solution {
public:
    int romanToInt(string s) 
    {
        std::map<std::string, int> query = {{"I", 1}, {"V", 5}, {"X", 10}, {"L", 50}, {"C", 100}, {"D", 500},{"M", 1000}};
        std::map<std::string, int> spe_query = {{"IV", 4}, {"IX", 9}, {"XL", 40}, {"XC", 90}, {"CD", 400}, {"CM", 900}};
        int sum = 0;
        std::string i_before = "";
        int len = s.size();
        for(int i = 0; i < len; i++)
        {
            std::string item(1, s[i]);
            std::string s_two = i_before + item;
            if (spe_query.count(s_two))
            {
                sum = sum + spe_query[s_two] - query[i_before];
            }
            else
            {
                sum = sum + query[item];
            }
            i_before = item;
        }
        return sum;

    }
}; 

class Solution {
public:
    int romanToInt(string s) 
    {
        std::map<char, int> query = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500},{'M', 1000}};
        int sum = query[s[0]];
        int len = s.size();
        for(int i = 1; i < len; i++)
        {
            if(query[s[i-1]] < query[s[i]])
            {
                sum = sum + query[s[i]] - query[s[i-1]] * 2;
            }
            else
            {
                sum += query[s[i]];
            }
        }    
        return sum;

    }
};